/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package dk.redillusions.jdbc.proxy.statistics;

import java.sql.ResultSet;
import java.sql.SQLException;

import dk.redillusions.jdbc.proxy.PreparedStatementAdapterDelegate;

/**
 * Collects statistics for actions performed on a {@link java.sql.Statement}.
 * 
 * @author Sune Wettersteen
 * 
 */
public class StatementStatisticsDelegate extends PreparedStatementAdapterDelegate {
    /**
     * Contains statistics about the {@link java.sql.Statement}.
     */
    private final IStatementStatisticsCollector statementStatisticsCollector;

    /**
     * The number of batch queries pending to be executed or cleared.
     */
    private long currentBatchQueriesPending = 0;

    /**
     * Constructor.
     * 
     * @param statementStatisticsCollector
     *            The {@link IStatementStatisticsCollector} to report statistics
     *            to
     */
    StatementStatisticsDelegate(final IStatementStatisticsCollector statementStatisticsCollector) {
        this.statementStatisticsCollector = statementStatisticsCollector;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final void addBatch(final String sql) throws SQLException {
        this.statementStatisticsCollector.increaseBatchQueriesAdded();
        this.currentBatchQueriesPending++;

        super.addBatch(sql);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final void clearBatch() throws SQLException {
        this.currentBatchQueriesPending = 0;

        super.clearBatch();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean execute(final String sql) throws SQLException {
        this.statementStatisticsCollector.increaseExecuteInvocations();

        return super.execute(sql);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {
        this.statementStatisticsCollector.increaseExecuteInvocations();

        return super.execute(sql, autoGeneratedKeys);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean execute(final String sql, final int[] columnIndexes) throws SQLException {
        this.statementStatisticsCollector.increaseExecuteInvocations();

        return super.execute(sql, columnIndexes);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean execute(final String sql, final String[] columnNames) throws SQLException {
        this.statementStatisticsCollector.increaseExecuteInvocations();

        return super.execute(sql, columnNames);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final int[] executeBatch() throws SQLException {
        this.statementStatisticsCollector.increaseBatchQueriesExecuted(this.currentBatchQueriesPending);
        this.currentBatchQueriesPending = 0;

        return super.executeBatch();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final ResultSet executeQuery(final String sql) throws SQLException {
        this.statementStatisticsCollector.increaseExecuteQueryInvocations();

        return super.executeQuery(sql);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final int executeUpdate(final String sql) throws SQLException {
        this.statementStatisticsCollector.increaseExecuteUpdateInvocations();

        return super.executeUpdate(sql);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
        this.statementStatisticsCollector.increaseExecuteUpdateInvocations();

        return super.executeUpdate(sql, autoGeneratedKeys);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
        this.statementStatisticsCollector.increaseExecuteUpdateInvocations();

        return super.executeUpdate(sql, columnIndexes);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
        this.statementStatisticsCollector.increaseExecuteUpdateInvocations();

        return super.executeUpdate(sql, columnNames);
    }

}
